package com.mao.dao.organization;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.action.jsonvo.organization.JsonVoOrganization;
import com.mao.dao.BaseDAOHibernate;
import com.mao.model.organization.Organization;
import com.mao.service.impl.organization.OrganizationCondition;
import com.mao.util.Validator;

@Component("organizationDao")
public class OrganizationDao extends BaseDAOHibernate {
	public List<Organization> getAllOrganization() {
		Session session = this.getSession();
		String hql = "from Organization t where t.parent.id = 0 and t.status = " + Organization.STATUS_NORMAL;
		Query query = session.createQuery(hql);
		return query.list();
	}

	public Organization saveOrganization(Organization o) {
		getHibernateTemplate().save(o);
		return o;
	}

	public Organization updateOrganization(Organization o) {
		getHibernateTemplate().update(o);
		return o;
	}
	
	public Organization getOrganizationById(long id) {
		Organization o = (Organization) this.getHibernateTemplate().get(Organization.class, id);
		return o;
	}
	
	// 获取往来单位类别下的所有往来单位
	public List<JsonVoOrganization> getOrgsByOrgClass(OrganizationCondition organizationCondition, long orgClassId,Integer page,Integer rows){
		Session session = this.getSession();
		String hql = "From Organization o where o.status=:status";
		if(!Validator.isEmpty(organizationCondition.getName()))
			hql += " and o.name like :name ";
		if(!Validator.isEmpty(organizationCondition.getContactPerson()))
			hql += " and o.contactPerson like :contactPerson ";
		if(organizationCondition.getStoreId() > 0){
			hql += " and o.store.id=" + organizationCondition.getStoreId();
		} 
		if(!Validator.isEmpty(organizationCondition.getTreePath()))
			hql += " and o.oc.treePath like :treePath";
		hql += " order by o.id desc";
		Query query = session.createQuery(hql);
		if(organizationCondition.getName() != null && organizationCondition.getName().length() > 0)
			query.setParameter("name", "%"+organizationCondition.getName().trim()+"%");
		if(organizationCondition.getContactPerson() != null && organizationCondition.getContactPerson().length() > 0)
			query.setParameter("contactPerson", "%"+organizationCondition.getContactPerson().trim()+"%");
		if(!Validator.isEmpty(organizationCondition.getTreePath()))
			query.setParameter("treePath", "%"+organizationCondition.getTreePath()+"%");
			
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		query.setParameter("status", Organization.STATUS_NORMAL);
		List<Organization> list = query.list();
		List<JsonVoOrganization> result = new ArrayList<JsonVoOrganization>();
		JsonVoOrganization jvo = null;
		for(Organization o : list){
			jvo = new JsonVoOrganization();
			jvo.setId(o.getId());
			jvo.setName(o.getName());
			jvo.setBankNo(o.getBankNo());
			jvo.setContactPerson(o.getContactPerson());
			jvo.setPhone(o.getPhone());
			jvo.setFax(o.getFax());
			jvo.setEmail(o.getEmail());
			jvo.setZipCode(o.getZipCode());
			jvo.setAddress(o.getAddress());
			jvo.setOrgClassId(o.getOc().getId());
			jvo.setOrgClassName(o.getOc().getName());
			jvo.setNote(o.getNote());
			result.add(jvo);
		}
		return result;
	}
	
	public int getOrgsByOrgClassgCount(OrganizationCondition organizationCondition, long orgClassId){
		Session session = this.getSession();
		String hql = "select count(id) From Organization o where o.status=:status";
		if(!Validator.isEmpty(organizationCondition.getName()))
			hql += " and o.name like :name ";
		if(!Validator.isEmpty(organizationCondition.getContactPerson()))
			hql += " and o.contactPerson like :contactPerson ";
//		if(orgClassId != 0){
//			hql += " and o.oc.id=" + orgClassId;
//		}
		if(organizationCondition.getStoreId() > 0){
			hql += " and o.store.id=" + organizationCondition.getStoreId();
		}
		if(!Validator.isEmpty(organizationCondition.getTreePath()))
			hql += " and o.oc.treePath like :treePath";
		
		Query query = session.createQuery(hql);
		if(organizationCondition.getName() != null && organizationCondition.getName().length() > 0)
			query.setParameter("name", "%"+organizationCondition.getName().trim()+"%");
		if(organizationCondition.getContactPerson() != null && organizationCondition.getContactPerson().length() > 0)
			query.setParameter("contactPerson", "%"+organizationCondition.getContactPerson().trim()+"%");
		if(!Validator.isEmpty(organizationCondition.getTreePath()))
			query.setParameter("treePath", "%"+organizationCondition.getTreePath()+"%");
		
		query.setParameter("status", Organization.STATUS_NORMAL);
		List<Organization> list = query.list();
		return ((Long)query.uniqueResult()).intValue(); 
	}
	
	public String valDelOrg(long id){
		Session session = this.getSession();
		String hql = "from ProductDeliveryVoucher pdv,ProductWareHouseVoucher whv where (pdv.voucher.org.id = :id or whv.voucher.org.id = :id)";
		Query query = session.createQuery(hql);
		query.setParameter("id", id);
		List list = query.list();
		if (list.size() > 0) {
			return "1";
		}
		return "0";
	}
}
